home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCollec / Sources / FWOrdCol.cpp < prev   
Encoding:
C/C++ Source or Header  |  1995-11-08  |  14.0 KB  |  464 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        FWOrdCol.cpp
  3.  
  4.     Contains:    Implementation of FW_CPrivOrderedCollection and FW_COrderedCollectionIterator
  5.  
  6.     Written by:    Richard Rodseth
  7.  
  8.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <4>     6/20/94    RR        ODMemoryHeap* -> ODMemoryHeapID
  13.          <3>     6/15/94    NP        ODHeap change.
  14.          <2>      6/9/94    RR        Remove ASLM Stuff.
  15.          <3>     3/15/94    MB        Changes to support SCpp/ASLM builds,
  16.                                     #1150864.
  17.          <2>     2/15/94    CG        #1143680 - OrdColls now allocated on
  18.                                     specified heap.
  19.          <8>      2/7/94    JA        Tiger Team Makeover!
  20.          <7>      2/2/94    NP        Added support for allocating internal
  21.                                     structures in given heap.
  22.          <6>     1/19/94    NP        Fixed Remove to delete the link.
  23.          <5>    11/19/93    NP        Moved definition of FW_CPrivValueLink to FWOrdCol.h
  24.          <4>    10/28/93    RR        Removed ambiguous constructor
  25.          <3>     8/13/93    CG        Added casting required by CFront.
  26.          <1>     8/13/93    RCR        first checked in
  27.  
  28.     To Do:
  29. */
  30.  
  31. #include "FWFound.hpp"
  32.  
  33. #ifndef FWORDCOL_H
  34. #include "FWOrdCol.h"
  35. #endif
  36.  
  37. #if FW_LIB_EXPORT_PRAGMAS
  38. #pragma lib_export on
  39. #endif
  40.  
  41. #ifdef FW_BUILD_MAC
  42. #pragma segment fwcollec
  43. #endif
  44.  
  45. //======================================================================================
  46. // Class FW_CPrivValueLink - Implementation
  47. //======================================================================================
  48.  
  49. FW_CPrivValueLink::FW_CPrivValueLink(FW_ElementType value)
  50. {
  51.     fValue = value;
  52. }
  53.  
  54. FW_CPrivValueLink::~FW_CPrivValueLink()
  55. {
  56. }
  57.  
  58. //======================================================================================
  59. // Class FW_CPrivOrderedCollection
  60. //======================================================================================
  61.  
  62. //------------------------------------------------------------------------------
  63. // FW_CPrivOrderedCollection::FW_CPrivOrderedCollection
  64. //------------------------------------------------------------------------------
  65.  
  66. FW_CPrivOrderedCollection::FW_CPrivOrderedCollection()
  67. {
  68.     fHeap = NULL;
  69. }
  70.  
  71. //------------------------------------------------------------------------------
  72. // FW_CPrivOrderedCollection::FW_CPrivOrderedCollection
  73. //------------------------------------------------------------------------------
  74.  
  75. FW_CPrivOrderedCollection::FW_CPrivOrderedCollection(void* where)
  76. {
  77.     fHeap = where;
  78. }
  79.  
  80. // FW_CPrivOrderedCollection::~FW_CPrivOrderedCollection
  81. //------------------------------------------------------------------------------
  82.  
  83. FW_CPrivOrderedCollection::~FW_CPrivOrderedCollection()
  84. {
  85.     this->RemoveAll();
  86. }
  87.  
  88. //------------------------------------------------------------------------------
  89. // FW_CPrivOrderedCollection::AddFirst
  90. //------------------------------------------------------------------------------
  91.  
  92. void FW_CPrivOrderedCollection::AddFirst(FW_ElementType element)
  93. {
  94.     FW_CPrivValueLink* newLink = this->CreateNewLink(element);
  95.     fImplementation.AddFirst(newLink);
  96. }
  97.  
  98. //------------------------------------------------------------------------------
  99. // FW_CPrivOrderedCollection::AddLast
  100. //------------------------------------------------------------------------------
  101.  
  102. void FW_CPrivOrderedCollection::AddLast(FW_ElementType element)
  103. {
  104.     FW_CPrivValueLink* newLink = CreateNewLink(element);
  105.     fImplementation.AddLast(newLink);
  106. }
  107.  
  108.  
  109. //------------------------------------------------------------------------------
  110. // FW_CPrivOrderedCollection::AddBefore
  111. //------------------------------------------------------------------------------
  112.  
  113. void FW_CPrivOrderedCollection::AddBefore(FW_ElementType existing, FW_ElementType tobeadded)
  114. {
  115.     FW_CPrivLinkedListIterator iter(&fImplementation);
  116.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
  117.     while (aLink != NULL)
  118.     {
  119.         FW_ElementType v = ((FW_CPrivValueLink*) aLink)->GetValue();
  120.  
  121.         if (this->ElementsMatch(v,existing))    
  122.         {
  123.             FW_CPrivValueLink* newLink = CreateNewLink(tobeadded);
  124.             fImplementation.AddBefore(*aLink, newLink);
  125.             aLink = NULL;
  126.         }
  127.         else
  128.             aLink = (FW_CPrivValueLink*) iter.Next();
  129.     }
  130. }
  131.  
  132. //------------------------------------------------------------------------------
  133. // FW_CPrivOrderedCollection::AddAfter
  134. //------------------------------------------------------------------------------
  135.  
  136. void FW_CPrivOrderedCollection::AddAfter(FW_ElementType existing, FW_ElementType tobeadded)
  137. {
  138.     FW_CPrivLinkedListIterator iter(&fImplementation);
  139.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
  140.     while (aLink != NULL)
  141.     {
  142.         FW_ElementType v = ((FW_CPrivValueLink*) aLink)->GetValue();
  143.  
  144.         if (this->ElementsMatch(v,existing))    
  145.         {
  146.             FW_CPrivValueLink* newLink = CreateNewLink(tobeadded);
  147.             fImplementation.AddAfter(*aLink, newLink);
  148.             aLink = NULL;
  149.         }
  150.         else
  151.             aLink = (FW_CPrivValueLink*) iter.Next();
  152.     }
  153. }
  154.  
  155. //------------------------------------------------------------------------------
  156. // FW_CPrivOrderedCollection::After
  157. //------------------------------------------------------------------------------
  158.  
  159. FW_ElementType FW_CPrivOrderedCollection::After(FW_ElementType existing)
  160. {
  161.     FW_CPrivValueLink* linkAfter = NULL;
  162.  
  163.     FW_CPrivLinkedListIterator iter(&fImplementation);
  164.  
  165.     for (FW_CPrivValueLink* link = (FW_CPrivValueLink*) iter.First(); iter.IsNotComplete(); link = (FW_CPrivValueLink*) iter.Next())
  166.     {
  167.         FW_ElementType v = ((FW_CPrivValueLink*) link)->GetValue();
  168.  
  169.         if (this->ElementsMatch(v,existing))    
  170.         {
  171.             linkAfter = (FW_CPrivValueLink*) fImplementation.After(*link);
  172.             break;
  173.         }
  174.     }
  175.     
  176.     return linkAfter ? linkAfter->GetValue() : (FW_ElementType)NULL;
  177. }
  178.  
  179. //------------------------------------------------------------------------------
  180. // FW_CPrivOrderedCollection::Before
  181. //------------------------------------------------------------------------------
  182.  
  183. FW_ElementType FW_CPrivOrderedCollection::Before(FW_ElementType existing)
  184. {
  185.     FW_CPrivValueLink* linkBefore = NULL;
  186.  
  187.     FW_CPrivLinkedListIterator iter(&fImplementation);
  188.  
  189.     for (FW_CPrivValueLink* link = (FW_CPrivValueLink*) iter.First(); iter.IsNotComplete(); link = (FW_CPrivValueLink*) iter.Next())
  190.     {
  191.         FW_ElementType v = ((FW_CPrivValueLink*) link)->GetValue();
  192.  
  193.         if (this->ElementsMatch(v,existing))    
  194.         {
  195.             linkBefore = (FW_CPrivValueLink*) fImplementation.Before(*link);
  196.             break;
  197.         }
  198.     }
  199.     
  200.     return linkBefore ? linkBefore->GetValue() : (FW_ElementType)NULL;
  201. }
  202.  
  203. //------------------------------------------------------------------------------
  204. // FW_CPrivOrderedCollection::First
  205. //------------------------------------------------------------------------------
  206.  
  207. FW_ElementType FW_CPrivOrderedCollection::First()
  208. {
  209.     FW_CPrivValueLink* firstLink = (FW_CPrivValueLink*) fImplementation.First();
  210.     return firstLink ? firstLink->GetValue() : (FW_ElementType)NULL;
  211. }
  212.  
  213. //------------------------------------------------------------------------------
  214. // FW_CPrivOrderedCollection::Last
  215. //------------------------------------------------------------------------------
  216.  
  217. FW_ElementType FW_CPrivOrderedCollection::Last()
  218. {
  219.     FW_CPrivValueLink* lastLink = (FW_CPrivValueLink*) fImplementation.Last();
  220.     return lastLink ? lastLink->GetValue() : (FW_ElementType)NULL;
  221. }
  222.  
  223. //------------------------------------------------------------------------------
  224. // FW_CPrivOrderedCollection::RemoveFirst
  225. //------------------------------------------------------------------------------
  226.  
  227. FW_ElementType    FW_CPrivOrderedCollection::RemoveFirst()
  228. {
  229.     FW_ElementType value = NULL;
  230.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) fImplementation.RemoveFirst();
  231.     if (aLink != NULL)
  232.     {
  233.         value = aLink->GetValue();
  234.         delete aLink;
  235.     }
  236.     return value;
  237. }
  238.  
  239. //------------------------------------------------------------------------------
  240. // FW_CPrivOrderedCollection::RemoveLast
  241. //------------------------------------------------------------------------------
  242.  
  243. FW_ElementType    FW_CPrivOrderedCollection::RemoveLast()
  244. {
  245.     FW_ElementType value = NULL;
  246.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) fImplementation.RemoveLast();
  247.     if (aLink != NULL)
  248.     {
  249.         value = aLink->GetValue();
  250.         delete aLink;
  251.     }
  252.     return value;
  253. }
  254.  
  255. //------------------------------------------------------------------------------
  256. // FW_CPrivOrderedCollection::Remove
  257. //------------------------------------------------------------------------------
  258.  
  259. void FW_CPrivOrderedCollection::Remove(FW_ElementType existing)
  260. {
  261.     FW_CPrivLinkedListIterator iter(&fImplementation);
  262.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
  263.     while (aLink != NULL)
  264.     {
  265.         FW_ElementType v = ((FW_CPrivValueLink*) aLink)->GetValue();
  266.  
  267.         if (this->ElementsMatch(v,existing))
  268.         {
  269.             fImplementation.Remove(*aLink);
  270.             delete aLink;
  271.             aLink = NULL;    
  272.         }
  273.         else
  274.             aLink = (FW_CPrivValueLink*) iter.Next();
  275.     }    
  276. }
  277.  
  278. //------------------------------------------------------------------------------
  279. // FW_CPrivOrderedCollection::RemoveAll
  280. //------------------------------------------------------------------------------
  281.  
  282. void FW_CPrivOrderedCollection::RemoveAll()
  283. {
  284.     FW_CPrivLink* link = fImplementation.RemoveFirst();
  285.     while (link != NULL)
  286.     {
  287.         delete link;
  288.         link = fImplementation.RemoveFirst();
  289.     }
  290. }
  291.  
  292. /*
  293. // this is too dangerous because the destructor of value will never be called
  294. //------------------------------------------------------------------------------
  295. // FW_CPrivOrderedCollection::DeleteAll
  296. //------------------------------------------------------------------------------
  297.  
  298. void FW_CPrivOrderedCollection::DeleteAll()
  299. {
  300.     FW_CPrivLink* link = fImplementation.RemoveFirst();
  301.     while (link != NULL)
  302.     {
  303.         FW_ElementType value = ((FW_CPrivValueLink*) link)->GetValue();
  304.         delete value;
  305.         delete link;
  306.         link = fImplementation.RemoveFirst();
  307.     }
  308. }
  309. */
  310.  
  311. //------------------------------------------------------------------------------
  312. // FW_CPrivOrderedCollection::Contains
  313. //------------------------------------------------------------------------------
  314.  
  315. FW_Boolean    FW_CPrivOrderedCollection::Contains(FW_ElementType existing)
  316. {
  317.     FW_CPrivLinkedListIterator iter(&fImplementation);
  318.     FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
  319.     while (aLink != NULL)
  320.     {
  321.         FW_ElementType v = ((FW_CPrivValueLink*) aLink)->GetValue();
  322.  
  323.         if (this->ElementsMatch(v,existing))
  324.         {
  325.             return TRUE;    
  326.         }
  327.         else
  328.             aLink = (FW_CPrivValueLink*) iter.Next();
  329.     }    
  330.     return FALSE;
  331. }
  332.  
  333. //------------------------------------------------------------------------------
  334. // FW_CPrivOrderedCollection::CreateIterator
  335. //------------------------------------------------------------------------------
  336.  
  337. FW_COrderedCollectionIterator* FW_CPrivOrderedCollection::CreateIterator()
  338. {
  339. //    return new(fHeap) FW_COrderedCollectionIterator(this);
  340.     return new FW_COrderedCollectionIterator(this);
  341. }
  342.  
  343.  
  344. //------------------------------------------------------------------------------
  345. // FW_CPrivOrderedCollection::CreateNewLink
  346. //------------------------------------------------------------------------------
  347.  
  348. FW_CPrivValueLink*    FW_CPrivOrderedCollection::CreateNewLink(FW_ElementType value) const
  349. {
  350. //    return new (fHeap) FW_CPrivValueLink(value);
  351.     return new FW_CPrivValueLink(value);
  352. }
  353.  
  354. //------------------------------------------------------------------------------
  355. // FW_CPrivOrderedCollection::ElementsMatch
  356. //------------------------------------------------------------------------------
  357.  
  358. FW_Boolean    FW_CPrivOrderedCollection::ElementsMatch(FW_ElementType v1,FW_ElementType v2) const
  359. {
  360.     return (v1 == v2);
  361. }
  362.  
  363. //======================================================================================
  364. // FW_COrderedCollectionIterator
  365. //======================================================================================
  366.  
  367. //------------------------------------------------------------------------------
  368. // FW_COrderedCollectionIterator::FW_COrderedCollectionIterator
  369. //------------------------------------------------------------------------------
  370.  
  371. FW_COrderedCollectionIterator::FW_COrderedCollectionIterator(FW_CPrivOrderedCollection* collection)    
  372.     : fImplementation(collection ? &(collection->fImplementation) : (FW_CPrivLinkedList*)NULL)
  373. {
  374.     fCollection =  collection;
  375. }
  376.  
  377. //------------------------------------------------------------------------------
  378. // FW_COrderedCollectionIterator::~FW_COrderedCollectionIterator
  379. //------------------------------------------------------------------------------
  380.  
  381. FW_COrderedCollectionIterator::~FW_COrderedCollectionIterator()                        
  382. {
  383. }
  384.  
  385. //------------------------------------------------------------------------------
  386. // FW_COrderedCollectionIterator::First
  387. //------------------------------------------------------------------------------
  388.  
  389. FW_ElementType    FW_COrderedCollectionIterator::First()
  390. {
  391.     if (fCollection)
  392.     {
  393.         FW_CPrivValueLink* link = (FW_CPrivValueLink*) fImplementation.First();
  394.     
  395.         return link ? link->GetValue() : (FW_ElementType)NULL;
  396.     }
  397.     else
  398.         return NULL;
  399. }
  400.  
  401. //------------------------------------------------------------------------------
  402. // FW_COrderedCollectionIterator::Next
  403. //------------------------------------------------------------------------------
  404.  
  405. FW_ElementType    FW_COrderedCollectionIterator::Next()
  406. {        
  407.     if (fCollection)
  408.     {
  409.         FW_CPrivValueLink* link = (FW_CPrivValueLink*) fImplementation.Next();
  410.     
  411.         return link ? link->GetValue() : (FW_ElementType)NULL;
  412.     }
  413.     else
  414.         return NULL;
  415. }
  416.  
  417. //------------------------------------------------------------------------------
  418. // FW_COrderedCollectionIterator::Last
  419. //------------------------------------------------------------------------------
  420.  
  421. FW_ElementType    FW_COrderedCollectionIterator::Last()
  422. {
  423.     if (fCollection)
  424.     {
  425.         FW_CPrivValueLink* link = (FW_CPrivValueLink*) fImplementation.Last();
  426.     
  427.         return link ? link->GetValue() : (FW_ElementType)NULL;
  428.     }
  429.     else
  430.         return NULL;
  431. }
  432.  
  433. //------------------------------------------------------------------------------
  434. // FW_COrderedCollectionIterator::Previous
  435. //------------------------------------------------------------------------------
  436.  
  437. FW_ElementType    FW_COrderedCollectionIterator::Previous()
  438. {
  439.     if (fCollection)
  440.     {
  441.         FW_CPrivValueLink* link = (FW_CPrivValueLink*) fImplementation.Previous();
  442.     
  443.         return link ? link->GetValue() : (FW_ElementType)NULL;
  444.     }
  445.     else
  446.         return NULL;
  447. }
  448.  
  449. //------------------------------------------------------------------------------
  450. // FW_COrderedCollectionIterator::IsNotComplete
  451. //------------------------------------------------------------------------------
  452.  
  453. FW_Boolean    FW_COrderedCollectionIterator::IsNotComplete()
  454. {
  455.     return fCollection ? fImplementation.IsNotComplete() : FALSE;
  456. }
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.